import factory.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.io.*;

public class Main {
    public static void main(String[] args) {
         try {
            String classNames = ReadClassNameFromXML();
            String[] classNamesArray = classNames.split("\\n");

            for (String className : classNamesArray) {
                // 反射机制
                Class<?> classReflection = Class.forName(className);
                ICarFactory factory = (ICarFactory) classReflection.newInstance();
                Car car;
                if (className.equals("factory.FactoryBaoma")) {
                    car = new EntityBaoma("宝马x7");
                } else if (className.equals("factory.FactoryBenchi")) {
                    car = new EntityBenchi("迈巴赫s600");
                } else if(className.equals("factory.FactoryXiaomi")){
                    car = new EntityXiaomi("小米su7");
                }else {
                    car = factory.carFactoryMethod();
                }
                System.out.println(car.showInfo());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static String ReadClassNameFromXML() {
        StringBuilder classNames = new StringBuilder();
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("config.xml"));

            // 获取包含类名的文本节点
            NodeList nl = doc.getElementsByTagName("className");
            for (int i = 0; i < nl.getLength(); i++) {
                Node classNode = nl.item(i).getFirstChild();
                classNames.append(classNode.getNodeValue()).append("\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return classNames.toString();
    }
}